﻿using ControlzEx.Theming;
using FieldTools.Client.Command;
using FieldTools.Client.Model;
using FieldTools.Client.Model.Window;
using FieldTools.Client.View;
using FieldTools.Client.View.Control;
using FieldTools.Client.View.Window;
using FieldTools.Util;
using MahApps.Metro.Controls.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;

namespace FieldTools.Client.ViewModel.Window
{
    /// <summary>
    /// 工具盒子视图模型
    /// </summary>
    public class ToolsBoxViewModel : NotificationObject, ISingletonDependency
    {
        /// <summary>
        /// 
        /// </summary>
        public ToolsBoxViewModel()
        {
            //this._dialogCoordinator = dialogCoordinator;
            // create accent color menu items for the demo
            this.AccentColors = ThemeManager.Current.Themes
                                            .GroupBy(x => x.ColorScheme)
                                            .OrderBy(a => a.Key)
                                            .Select(a => new AccentColorMenuData { Name = a.Key, ColorBrush = a.First().ShowcaseBrush })
                                            .ToList();

            // create metro theme color menu items for the demo
            this.AppThemes = ThemeManager.Current.Themes
                                         .GroupBy(x => x.BaseColorScheme)
                                         .Select(x => x.First())
                                         .Select(a => new AppThemeMenuData() { Name = a.BaseColorScheme, BorderColorBrush = a.Resources["MahApps.Brushes.ThemeForeground"] as Brush, ColorBrush = a.Resources["MahApps.Brushes.ThemeBackground"] as Brush })
                                         .ToList();
            this.SetCardMenu();
            this.Title = "测试主页菜单";
            var accenColor = CacheHelper.GetCache("AccentColor");
            if(accenColor != null)
            {
                ThemeManager.Current.ChangeThemeColorScheme(Application.Current, accenColor.ToString());
            }

            var appTheme = CacheHelper.GetCache("AppTheme");
            if (appTheme != null)
            {
                ThemeManager.Current.ChangeThemeBaseColor(Application.Current, appTheme.ToString());
            }
        }

        /// <summary>
        /// 着重色菜单数据
        /// </summary>
        public class AccentColorMenuData
        {
            public string? Name { get; set; }

            public Brush? BorderColorBrush { get; set; }

            public Brush? ColorBrush { get; set; }

            public AccentColorMenuData()
            {
            }

            private BaseCommand changeAccentCommand;
            /// <summary>
            /// 
            /// </summary>
            public BaseCommand ChangeAccentCommand
            {
                get
                {
                    changeAccentCommand ??= new BaseCommand(new Action<object>(o =>
                        {
                            this.DoChangeTheme(o);
                        }));
                    return changeAccentCommand;
                }
            }

            protected virtual void DoChangeTheme(object sender)
            {
                ThemeManager.Current.ChangeThemeColorScheme(Application.Current, this.Name);
                CacheHelper.SetCache("AccentColor", this.Name);
            }
        }
        /// <summary>
        /// 主题色菜单数据
        /// </summary>
        public class AppThemeMenuData : AccentColorMenuData
        {
            protected override void DoChangeTheme(object sender)
            {
                ThemeManager.Current.ChangeThemeBaseColor(Application.Current, this.Name);
                CacheHelper.SetCache("AppTheme", this.Name);
            }
        }
       
        private readonly IDialogCoordinator? _dialogCoordinator;
        int? _integerGreater10Property = 2;
        private bool _animateOnPositionChange = true;
        /// <summary>
        /// 着重色
        /// </summary>
        public List<AccentColorMenuData>? AccentColors { get; set; }
        /// <summary>
        /// 主题色
        /// </summary>
        public List<AppThemeMenuData>? AppThemes { get; set; }

        private string? noticeMsg = "";
        /// <summary>
        /// 底部通知消息
        /// </summary>
        public string? NoticeMsg
        {
            get{ return noticeMsg; }
            set { noticeMsg = value; this.RaisePropertyChanged(nameof(NoticeMsg)); }
        }
        private bool isHamburgerMenuPaneOpen = true;
        /// <summary>
        /// 
        /// </summary>
        public bool IsHamburgerMenuPaneOpen
        {
            get => this.isHamburgerMenuPaneOpen;
            set { isHamburgerMenuPaneOpen = value; this.RaisePropertyChanged(nameof(IsHamburgerMenuPaneOpen));}
        }

        private List<ControlCon>? mutControls;
        /// <summary>
        /// 工具菜单属性
        /// </summary>
        public List<ControlCon>? MutControls
        {
            get => this.mutControls;
            set { mutControls = value; this.RaisePropertyChanged(nameof(MutControls));}
        }

        private string title;
        /// <summary>
        /// 
        /// </summary>
        public string Title
        {
            get { return title; }
            set { title = value; this.RaisePropertyChanged(nameof(Title)); }
        }

        #region 事件

        #endregion

        #region 方法

        /// <summary>
        /// 设置卡片菜单
        /// </summary>
        private void SetCardMenu()
        {
            WindowControlManager.Register<BlankControl>("BlankControl");
            WindowControlManager.Register<HttpClientWindow>("HttpClientControl");
            WindowControlManager.Register<HttpServerWindow, HttpServerViewModel>("HttpServerControl", "HttpServerViewModel");
            WindowControlManager.Register<JSInterpreterWindow, JSInterpreterViewModel>("JSInterpreterControl", "JSInterpreterViewModel");
            WindowControlManager.Register<MyQQWindow, MyQQViewModel>("MyQQControl", "MyQQViewModel");
            WindowControlManager.Register<PersonWindow>("PersonControl");
            WindowControlManager.Register<WebViewWindow>("WebViewControl");
            WindowControlManager.Register<TCPClientWindow>("TCPClientControl");
            WindowControlManager.Register<TCPServerWindow>("TCPServerControl");
            var sa = System.IO.Directory.GetCurrentDirectory();
            MutControls = new List<ControlCon>();
            MutControls.AddRange(new List<ControlCon>()
            {
                new ControlCon()
                {
                    Title = "TCP服务端",
                    Name = "TCPServerControl",
                    ImgPath = $"{sa}/Resource/testpic2.png",
                    Remark = "一个简单的TCP请求测试工具（服务端），基于RRQMSocket项目（作者：若汝棋茗）"
                },new ControlCon()
                {
                    Title = "TCP客户端",
                    Name = "TCPClientControl",
                    ImgPath = $"{sa}/Resource/testpic1.png",
                    Remark = "一个简单的TCP请求测试工具（客户端），基于RRQMSocket项目（作者：若汝棋茗）"
                },new ControlCon()
                {
                    Title = "HTTP服务端",
                    Name = "HttpServerControl",
                    VMName = "HttpServerViewModel",
                    ImgPath = $"{sa}/Resource/testpic2.png",
                    Remark = "一个简单的HTTP请求测试工具（服务端）"
                },
                new ControlCon()
                {
                    Title = "HTTP客户端",
                    Name = "HttpClientControl",
                    ImgPath = $"{sa}/Resource/testpic1.png",
                    Remark = "一个简单的HTTP请求测试工具（客户端）"
                }
            });
            //基础工具
            //M2.Title = "基础工具";
            //M2.StateContent = "这些工具通常用于一般调试使用，为通用类型工具";
            //M2.ThemeColor = ClientConfig.ThemeColor;
            //M2.MutControls = new List<ControlCon>() {
            //    new ControlCon()
            //    {
            //        Title = "TCP服务端",
            //        Name = "TCPServerControl",
            //        ImgPath = $"{sa}/Resource/testpic2.png",
            //        Remark = "一个简单的TCP请求测试工具（服务端），基于RRQMSocket项目（作者：若汝棋茗）"
            //    },new ControlCon()
            //    {
            //        Title = "TCP客户端",
            //        Name = "TCPClientControl",
            //        ImgPath = $"{sa}/Resource/testpic1.png",
            //        Remark = "一个简单的TCP请求测试工具（客户端），基于RRQMSocket项目（作者：若汝棋茗）"
            //    },new ControlCon()
            //    {
            //        Title = "HTTP服务端",
            //        Name = "HttpServerControl",
            //        VMName = "HttpServerViewModel",
            //        ImgPath = $"{sa}/Resource/testpic2.png",
            //        Remark = "一个简单的HTTP请求测试工具（服务端）"
            //    },
            //    new ControlCon()
            //    {
            //        Title = "HTTP客户端",
            //        Name = "HttpClientControl",
            //        ImgPath = $"{sa}/Resource/testpic1.png",
            //        Remark = "一个简单的HTTP请求测试工具（客户端）"
            //    }
            //};
            ////数据对接
            //M3.Title = "数据对接类工具";
            //M3.StateContent = "这些工具通常是定制开发出来用于对接指定软件数据的，可以简单理解为第三方操作端";
            //M3.MutControls = new List<ControlCon>() {
            //    new ControlCon()
            //    {
            //        Title = "MyQQ框架",
            //        Name = "MyQQControl",
            //        VMName = "MyQQViewModel",
            //        ImgPath = $"{sa}/Resource/testpic1.png",
            //        Remark = "基于MyQQ机器人框架项目官方HttpApi（作者：Daen）插件开发的简易对接客户端，支持自定义回复、预定义接口调用、自定义JS脚本等功能"
            //    }
            //};
            ////高级工具
            //M4.Title = "高级工具";
            //M4.StateContent = "这些工具通常用于一些特定情况的开发，功能为半定制，可以实现相对更高级的操作";
            //M4.MutControls = new List<ControlCon>() {
            //    new ControlCon()
            //    {
            //        Title = "JS解释器",
            //        Name = "JSInterpreterControl",
            //        VMName = "JSInterpreterViewModel",
            //        ImgPath = $"{sa}/Resource/testpic2.png",
            //        Remark = "基于Jering.Javascript.NodeJS 项目（作者：JeremyTCD）开发的JS语句执行器，可以直接执行JS脚本语句和JS脚本文件"
            //    }
            //};
            ////内置浏览器
            //M5.Title = "内置浏览器工具";
            //M5.StateContent = "集成几款常用的浏览器，只具备基础网页访问功能，仅推荐在你手中没有特定浏览器的情况下临时测试使用";
            //M5.MutControls = new List<ControlCon>() {
            //    new ControlCon()
            //    {
            //        Title = "内置浏览器",
            //        Name = "WebViewControl",
            //        ImgPath = $"{sa}/Resource/testpic2.png",
            //        Remark = "微软官方WebView2库，Edge (Chromium)浏览器内核"
            //    }
            //};
            ////其他
            //M6.Title = "其他工具";
            //M6.StateContent = "不太好分类的一些东西";
            //M6.MutControls = new List<ControlCon>() {
            //    new ControlCon()
            //    {
            //        Title = "空白页面",
            //        Name = "BlankControl",
            //        ImgPath = $"{sa}/Resource/testpic1.png",
            //        Remark = "着姿势一个用来测试的空白玩意🙂🙂"
            //    }
            //};
        }

        #endregion
    }
}
